package com.cyan.app.workflow.activity.WHM;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.cyan.app.workflow.R;
import com.cyan.app.workflow.base.activity.BaseActivity;
import com.cyan.app.workflow.bean.ProgramLlLabBean;
import com.cyan.app.workflow.customview.PullDownView;
import com.cyan.app.workflow.util.AppUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by DXW on 2016-04-15.
 */
public class CxllLlProgramsActivity extends BaseActivity {

    public static final String TAG = "CxllLlProgramsActivity";
    private ProgramLlLabBean.ProgramLlBean manpahdTBean;
    private List<ProgramLlLabBean.ProgramLlDetailBean> manpahdBeans;
    private List<ProgramLlLabBean.ProgramLlDetailBean> manpahdBeansPrimal;
    private GdllLlProgramsAdapter adapter;
    private ListView prossinfo_lv;
    private PullDownView mPullDownView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.whm_cxll_ll_programs);
        titleBar();
        mPullDownView = (PullDownView) findViewById(R.id.whm_cxll_ll_programs_lv);
        prossinfo_lv = mPullDownView.getListView();
        // 隐藏并且禁用头部刷新
        mPullDownView.setHideHeader();
        mPullDownView.setHideFooter();

        manpahdTBean = (ProgramLlLabBean.ProgramLlBean)getIntent().getSerializableExtra("cxllPrograms");
        manpahdBeans=manpahdTBean.getProgramLlDetails();
        manpahdBeansPrimal = new ArrayList<ProgramLlLabBean.ProgramLlDetailBean>();
        for (int i = 0;i<manpahdBeans.size();i++){
            ProgramLlLabBean.ProgramLlDetailBean programLlDetailBean = new ProgramLlLabBean().new ProgramLlDetailBean();
            programLlDetailBean.setProgramName(manpahdBeans.get(i).getProgramName());
            programLlDetailBean.setAppNum(manpahdBeans.get(i).getAppNum());
            programLlDetailBean.setSelected(manpahdBeans.get(i).getSelected());
            manpahdBeansPrimal.add(programLlDetailBean);
        }

        adapter = new GdllLlProgramsAdapter(CxllLlProgramsActivity.this);
        prossinfo_lv.setAdapter(adapter);
    }
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    private void titleBar() {
        TextView mTextView = (TextView) findViewById(R.id.top_title_info);
        mTextView.setText("程序列表");
        Button back = (Button) findViewById(R.id.top_title_back);
        back.getBackground().setAlpha(255);// 透明度0~255透明度值 ，值越小越透明
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //返回按钮
                finish();
            }
        });
        Button mButton = (Button)findViewById(R.id.top_title_refres_with_icon);
        mButton.setVisibility(View.VISIBLE);
        mButton.setText("确定");
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int selected = 0;
                Double risqty = 0.00;
                for (int i = 0; i < manpahdBeans.size(); i++) {
                    if (!manpahdBeans.get(i).getSelected()) {
                        continue;
                    }
                    selected++;
                    if (manpahdBeans.get(i).getAppNum() > manpahdBeansPrimal.get(i).getAppNum()) {
                        AppUtils.showTip(CxllLlProgramsActivity.this, "输入数量不能大于原数量");
                        return;
                    }
                    risqty = risqty + manpahdBeans.get(i).getAppNum();
                }

                if (selected == 0 || risqty == 0) {
                    AppUtils.showTip(CxllLlProgramsActivity.this, "请选择发料工单和输入数量");
                } else {
                    for (int i = manpahdBeans.size() - 1; i >= 0; i--) {
                        if (manpahdBeans.get(i).getSelected()) {
                            continue;
                        }
                        manpahdBeans.remove(i);
                    }
                    manpahdTBean.setAppNum(risqty);
                    Intent intent = new Intent();
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("cxllProgramDetail", manpahdTBean);
                    intent.putExtras(bundle);
                    setResult(RESULT_OK, intent);
                    finish();
                }
            }
        });

    }

    public class GdllLlProgramsAdapter extends BaseAdapter {
        private LayoutInflater inflater = null;
        public GdllLlProgramsAdapter(Context context) {
            inflater = LayoutInflater.from(context);
        }
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return manpahdBeans.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder  = new ViewHolder();
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            layoutParams.gravity = Gravity.CENTER_VERTICAL;
            convertView = inflater.inflate(R.layout.whm_cxll_ll_programs_list, null);
            holder.gdll_mannos_manno = (TextView) convertView.findViewById(R.id.layout_list4_list_c2);
            holder.gdll_mannos_appnum = (EditText) convertView.findViewById(R.id.layout_list4_list_c3);
            holder.gdll_mannos_selected = (CheckBox) convertView.findViewById(R.id.layout_list4_list_c1);

            holder.gdll_mannos_manno.setText(manpahdBeans.get(position).getProgramName());
            holder.gdll_mannos_appnum.setText(manpahdBeans.get(position).getAppNum().toString());
            holder.gdll_mannos_selected.setChecked(manpahdBeans.get(position).getSelected());
            holder.gdll_mannos_selected.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    manpahdBeans.get(position).setSelected(isChecked);
                }
            });
            holder.gdll_mannos_appnum.addTextChangedListener(new TextWatcher() {
                Double num = 0.00;

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if (!s.toString().isEmpty()) {
                        try {
                            num = Double.parseDouble(s.toString());
                        } catch (Exception e) {
                            AppUtils.showTip(CxllLlProgramsActivity.this, "数量格式不正确");
                        }
                    }
                }
                @Override
                public void afterTextChanged(Editable s) {
                    manpahdBeans.get(position).setAppNum(num);
                }
            });
            convertView.setTag(holder);
            return convertView;
        }

        class ViewHolder {
            TextView gdll_mannos_manno;
            EditText gdll_mannos_appnum;
            CheckBox gdll_mannos_selected;
        }
    }
}

